<?php

class XuatController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
   
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteThu($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $TRUONG_ID=$_POST['TRUONG_ID'];
            $XUAT_NGAY_XUAT=$_POST['XUAT_NGAY_XUAT'];
            $XUAT_LY_DO_XUAT=$_POST['XUAT_LY_DO_XUAT'];
            $XUAT_TONG_TIEN_PHAI_THU=$_POST['XUAT_TONG_TIEN_PHAI_THU'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
             $hinhthucthanhtoan=$_POST['hinhthucthanhtoan'];
              $result=null;
             if($hinhthucthanhtoan=='1'){
            $result = $this->addThu($TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
             }
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                }
            }
             exit();
             
            
        } else if ($action == 'edit' ) {

            $XUAT_ID=$_POST['XUAT_ID'];
            $TRUONG_ID=$_POST['TRUONG_ID'];
            $XUAT_NGAY_XUAT=$_POST['XUAT_NGAY_XUAT'];
            $XUAT_LY_DO_XUAT=$_POST['XUAT_LY_DO_XUAT'];
            $XUAT_TONG_TIEN_PHAI_THU=$_POST['XUAT_TONG_TIEN_PHAI_THU'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
            
             $result1 = $this->deleteChiTietThu($XUAT_ID);
             
            $result2 = $this->deleteThu_chitiet($XUAT_ID);
            
            $result3 = $this->deleteThue_HoaHong($XUAT_ID);
            
            $result = $this->updateThu($XUAT_ID,$TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                }
            }
             exit();
            
            

            
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_xuat' . DS . 'views' . DS . 'xuat_add.php');
            
            $truongs=$this->getAllTruong();
            
              if ($truongs != null) {
                 
                foreach ($truongs as $truong) {

                     
                     $xtemplate->assign('TRUONG_ID', $truong->getTRUONG_ID());
                    $xtemplate->assign('TRUONG_TEN_TRUONG', $truong->getTRUONG_TEN_TRUONG());
                    $xtemplate->parse('main.truong');
                    
                }
            }
           $hanghoa = $this->getAllHangHoa();


            if ($hanghoa != null) {
                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());        
                    
                    $xtemplate->parse('main.hanghoa');
 
            }
            
            $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {

                    $xtemplate->assign('H_ID', $hanghoas_gia_soluongs->getID());
                    $xtemplate->assign('H_TEN', $hanghoas_gia_soluongs->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoas_gia_soluongs->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoas_gia_soluongs->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoas_gia_soluongs->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoas_gia_soluongs->getDON_GIA_LE());
                    $xtemplate->parse('main.hanghoas_gia_soluong');
                    
               
            }

            
             $xtemplate->assign('DATE', Utility::getCurrentDate());
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idxuat'];
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_xuat' . DS . 'views' . DS . 'xuat_edit.php');
            $thu = $this->getThuById($id);
//            Utility::messageBox($id);

            if ($thu != null) {
                
 
                    $xtemplate->assign('XUAT_ID', $thu->getXUAT_ID());
                    $xtemplate->assign('TRUONG_ID', $thu->getTRUONG_ID());
                    $xtemplate->assign('XUAT_NGAY_XUAT', $thu->getXUAT_NGAY_XUAT());
                    $xtemplate->assign('XUAT_LY_DO_XUAT', $thu->getXUAT_LY_DO_XUAT());
                    $xtemplate->assign('XUAT_TONG_TIEN_PHAI_THU', $thu->getXUAT_TONG_TIEN_PHAI_THU());
                     $truongs=$this->getAllTruong();
             $option_truong='';
              if ($truongs != null) {
                 
                foreach ($truongs as $truong) {

                     
                      if($thu->getTRUONG_ID()==$truong->getTRUONG_ID()){
                          $option_truong .="<option selected=selected value='".$truong->getTRUONG_ID()."'>".$truong->getTRUONG_TEN_TRUONG()."</option>";
                          }else{
                             $option_truong .="<option  value='".$truong->getTRUONG_ID()."'>".$truong->getTRUONG_TEN_TRUONG()."</option>";
                            
                          }
                    
                }
            }
                    
                    
                    $xtemplate->assign('TRUONG', $option_truong);
                    
                    
                    
                    
           $hanghoa = $this->getAllHangHoa();
            if ($hanghoa != null) {
                 
                
                    
                    $chitietthu=$this->getChiTietThuById($thu->getXUAT_ID(),$hanghoa->getID());
                    if($chitietthu!=null){
                        $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$chitietthu->getDON_GIA() );
                    $xtemplate->assign('SO_LUONG',$chitietthu->getSO_LUONG());
                    }else{
                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('SO_LUONG',0);
                    }
                    
                    
                    $xtemplate->parse('main.thu.hanghoa');
                    
                
            }
                    
                  $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {
                 
                

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    $xtemplate->parse('main.thu.hanghoas_gia_soluong');
                    
                
            }  
                    
                    
                    $xtemplate->parse('main.thu');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/xuat.php';
            $xtemplate = new XTemplate($viewFile);
            $xuats = $this->getAllXuat();


            if ($xuats != null) {
                foreach ($xuats as $xuat) {

                    $xtemplate->assign('XUAT_ID', $xuat->getXUAT_ID());
                     $xtemplate->assign('XUAT_NGAY_XUAT',Utility::formatDateTimeToView($xuat->getXUAT_NGAY_XUAT()) );
                    $xtemplate->assign('TRUONG', $this->getTruongById($xuat->getTRUONG_ID())->getTRUONG_TEN_TRUONG());
                    $xtemplate->assign('XUAT_TONG_TIEN_PHAI_THU',Utility::formatToCurrencyString($xuat->getXUAT_TONG_TIEN_PHAI_THU()) );
                     $xtemplate->assign('XUAT_TONG_TIEN_DA_THU',Utility::formatToCurrencyString($xuat->getXUAT_TONG_TIEN_DA_THU()) );
                    
                    
                    
                    
                    $xtemplate->parse('main.xuat');
                   
                }
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        
        
        
        
        
        
        
        
    }

 
     private function getHoaHongById($id,$truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getByID($id,$truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getArrayHoaHongById($truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getArrayByID($truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
   
     private function  getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
     
    
    
    
   
    
        private function updateThu($XUAT_ID,$TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $xuat = new Xuat();
                $xuat->setXUAT_ID($XUAT_ID);
                 $xuat->setTRUONG_ID($TRUONG_ID);
                
                 $date = new DateTime($XUAT_NGAY_XUAT);
                 $xuat->setXUAT_NGAY_XUAT($date->format("Y-m-d"));
                 $xuat->setXUAT_LY_DO_XUAT($XUAT_LY_DO_XUAT);
                 $xuat->setXUAT_TONG_TIEN_PHAI_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setXUAT_TONG_TIEN_DA_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setIS_ACTIVE(1);
                $xuat->update($db);

         
                 //Chi cho hoa hồng
                $hoahong=new Hoa_hong();
                $hoahongs=$hoahong->getByID_($TRUONG_ID,$db);
                if ($hoahongs != null) {
                 
                foreach ($hoahongs as $hoa_hong) {

                    $loaihoahong=new LoaiHoaHong();
                    $loaihoahong->setLHH_ID($hoa_hong->getLHH_ID());
                    $loaihoahongs=$loaihoahong->getByID($db);
                   
                    $trahoahong=new Tra_hoa_hong();
                    $SOTIEN=0;
                    if($loaihoahongs!=null){
                         
                        $trahoahong->setXUAT_ID($xuat->getXUAT_ID());
                        $trahoahong->setLHH_ID($loaihoahongs->getLHH_ID());
                        $SOLUONG_= explode(',', $SOLUONG);
                        if(($loaihoahongs->getLHH_PHAN_TRAM()==0) && ($loaihoahongs->getLHH_TIEN_MAT()!=NULL)){
                            $SOTIEN=($loaihoahongs->getLHH_TIEN_MAT())*$SOLUONG_[0];
                        }else{
                            $SOTIEN=(($loaihoahongs->getLHH_PHAN_TRAM())/100)*$XUAT_TONG_TIEN_PHAI_THU;
                        }
                        $trahoahong->setSO_TIEN($SOTIEN);
                        $trahoahong->insert($db);
//                        echo $SOTIEN;
//                        exit();
                    }
                    
                    $chi=new Chi();
                    $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
                    $chi->setCHI_SO_TIEN($SOTIEN);
                    $chi->setCHI_NOI_DUNG("Chi loại hoa hồng: ".$loaihoahongs->getLHH_TEN_LOAI());
                    $chi->setCHI_PHAN_LOAI("hoahong");
                    $chi->setIS_ACTIVE(1);
                    $chi->insert($db);
                    
                    
                    $chihoahong=new Chi_hoa_hong();
                    $chihoahong->setCHI_ID($chi->getCHI_ID());
                    $chihoahong->setTHH_ID($trahoahong->getTHH_ID());
                    $chihoahong->setSO_TIEN($SOTIEN);
                    $chihoahong->insert($db);
//                    echo 'a';
//                    exit();
                }
                }
                //Kết thúc chi hoa hồng
                
                //Chi thuế
                $truong=new Truong();
                $truong->setTRUONG_ID($TRUONG_ID);
                $truong->getByID($db);
                if($truong!=null){
                   $bep=new Bep();
                   $bep->setBEP_ID($truong->getBEP_ID());
                   $bep->getByID($db);
                   if($bep !=null){
                       
                       if($bep->getBEP_IS_THUE()==1){
                            $SOTIEN_THUE=0;
                            $trathue=new Tra_thue();
                            $trathue->setXUAT_ID($xuat->getXUAT_ID());
                            $SOTIEN_THUE=$XUAT_TONG_TIEN_PHAI_THU*0.052;
                            $trathue->setSO_TIEN($SOTIEN_THUE);
                            $trathue->insert($db);
                            
                            
                            $chi=new Chi();
                            $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
                            $chi->setCHI_SO_TIEN($SOTIEN_THUE);
                            $chi->setCHI_NOI_DUNG("Chi cho thuế");
                            $chi->setCHI_PHAN_LOAI("thue");
                            $chi->setIS_ACTIVE(1);
                            $chi->insert($db);
                            
                            
                            $chithue=new Chi_thue();
                            $chithue->setCHI_ID($chi->getCHI_ID());
                            $chithue->setTT_ID($trathue->getTT_ID());
                            $chithue->setSO_TIEN($SOTIEN_THUE);
                            $chithue->insert($db);
                       }
                   }
                }
                
               
                
                //Kết thúc chi thuế
                
                
                
                
                
                $thu=new Thu();
                $thu->setNGAY_THU($date->format("Y-m-d"));
                $thu->setSO_TIEN($XUAT_TONG_TIEN_PHAI_THU);
                $thu->setIS_ACTIVE(1);
                $thu->insert($db);
                
                $thubancom=new Thu_ban_com();
                $thubancom->setTHU_ID($thu->getTHU_ID());
                $thubancom->setXUAT_ID($xuat->getXUAT_ID());
                $thubancom->setSO_TIEN($XUAT_TONG_TIEN_PHAI_THU);
                
                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietxuat=new Chi_tiet_xuat();
                $chitietxuat->setXUAT_ID($xuat->getXUAT_ID());
                $chitietxuat->setHH_ID($ID_HH_[$i]);
                $chitietxuat->setDON_GIA($DON_GIA_[$i]);
                $chitietxuat->setSO_LUONG($SOLUONG_[$i]);
                $chitietxuat->setTHANH_TIEN($THANHTIEN_[$i]);
                $chitietxuat->setIS_ACTIVE(1);
                $chitietxuat->insert($db);
            }
                
                 
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    

    
    
    
   //----------------------đang sử dụng 
   
    private function deleteChiTietThu($values) {
        $db = new Database();
        try {            
           
                $chitietxuat = new Chi_tiet_xuat();
                $chitietxuat->setXUAT_ID($values);
                $chitietxuat->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    private function deleteThu_chitiet($values) {
        $db = new Database();
        try {            
           
                $thu_ban_com = new Thu_ban_com();
                $thu_ban_com->setXUAT_ID($values);
                $thu_ban_com->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
      private function getChiTietThuById($thu,$hanghoa) {
        $db = new Database();
        try {
            $chitietthu = new Chi_tiet_xuat();
            $result = $chitietthu->getByID($thu,$hanghoa,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
       private function getAllXuat() {
        $db = new Database();
        try {
            $xuat= new Xuat();

            $result = $xuat->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getThuById($id) {
        $db = new Database();
        try {
            $xuat = new Xuat();
            $xuat->setXUAT_ID($id);
            $result = $xuat->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
      private function getTruongById($id) {
        $db = new Database();
        try {
            $truong = new Truong();
            $truong->setTRUONG_ID($id);
            $result = $truong->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
     private function addThu($TRUONG_ID, $XUAT_NGAY_XUAT,$XUAT_LY_DO_XUAT,$XUAT_TONG_TIEN_PHAI_THU,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $xuat = new Xuat();
                $maxTuan=$xuat->getMaxID_($db);
                if($maxTuan->getXUAT_ID() ==''){
                    
                    $xuat->setTUAN_ID(1);   
                     $xuat->setTHANG_ID(1);
                }else{
                   $xuat_max=new Xuat();
                   $xuat_max->setXUAT_ID($maxTuan->getXUAT_ID());
                   $xuat_maxs=$xuat_max->getByID($db);
                   
                    if(Utility::getWeekDates($XUAT_NGAY_XUAT,$xuat_maxs->getXUAT_NGAY_XUAT())){
                        
                         $xuat->setTUAN_ID($xuat_maxs->getTUAN_ID());   
                         $xuat->setTHANG_ID($xuat_maxs->getTHANG_ID());
                          
                    }else{
                       
                        if($xuat_maxs->getTUAN_ID()<4){//echo $nhap_maxs->getTUAN_ID();exit();
                             $xuat->setTUAN_ID($xuat_maxs->getTUAN_ID()+1);   
                             $xuat->setTHANG_ID($xuat_maxs->getTHANG_ID());
                              
                        }else{
                             $xuat->setTUAN_ID(1);   
                             $xuat->setTHANG_ID($xuat_maxs->getTHANG_ID()+1);
                        }
                    }
                    
                }
                
                
                if($xuat->getMaxID($db)->getXUAT_SO_PHIEU_INT()!=''){
                $MaxId=($xuat->getMaxID($db)->getXUAT_SO_PHIEU_INT())+1;
                switch (strlen($MaxId)) {
                    case 1:
                        $xuat->setXUAT_SO_PHIEU_XUAT("PX00000".$MaxId.$xuat->getMaxID($db)->getXUAT_SO_PHIEU_XUAT());
                        break;
                   case 2:
                         $xuat->setXUAT_SO_PHIEU_XUAT("PX0000".$MaxId.$xuat->getMaxID($db)->getXUAT_SO_PHIEU_XUAT());
                        break;
                    case 3:
                         $xuat->setXUAT_SO_PHIEU_XUAT("PX000".$MaxId.$xuat->getMaxID($db)->getXUAT_SO_PHIEU_XUAT());
                        break;
                    case 4:
                         $xuat->setXUAT_SO_PHIEU_XUAT("PX00".$MaxId.$xuat->getMaxID($db)->getXUAT_SO_PHIEU_XUAT());
                        break;
                    case 5:
                         $xuat->setXUAT_SO_PHIEU_XUAT("PX0".$MaxId.$xuat->getMaxID($db)->getXUAT_SO_PHIEU_XUAT());
                        break;
                    case 6:
                         $xuat->setXUAT_SO_PHIEU_XUAT("PX".$MaxId.$xuat->getMaxID($db)->getXUAT_SO_PHIEU_XUAT());
                        break;

                    default:
                        break;
                }
                
                
                $xuat->setXUAT_SO_PHIEU_INT($MaxId);
                }else{
                    $xuat->setXUAT_SO_PHIEU_XUAT("PX000001".date("Y"));
                     $xuat->setXUAT_SO_PHIEU_INT(1);
                }
                
                 $xuat->setXUAT_ID(null);
                $xuat->setTRUONG_ID($TRUONG_ID);
                
                 $date = new DateTime($XUAT_NGAY_XUAT);
                 $xuat->setXUAT_NGAY_XUAT($date->format("Y-m-d"));
                 $xuat->setXUAT_LY_DO_XUAT($XUAT_LY_DO_XUAT);
                 $xuat->setXUAT_TONG_TIEN_PHAI_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setXUAT_TONG_TIEN_DA_THU($XUAT_TONG_TIEN_PHAI_THU);
                 $xuat->setIS_ACTIVE(1);
                $xuat->insert($db);

//                echo 'a';exit();
                 //Chi cho hoa hồng
                $hoahong=new Hoa_hong();
                $hoahongs=$hoahong->getByID_($TRUONG_ID,$db);
                if ($hoahongs != null) {
                 
                foreach ($hoahongs as $hoa_hong) {

                    $loaihoahong=new LoaiHoaHong();
                    $loaihoahong->setLHH_ID($hoa_hong->getLHH_ID());
                    $loaihoahongs=$loaihoahong->getByID($db);
                   
                    $trahoahong=new Tra_hoa_hong();
                    $SOTIEN=0;
                    if($loaihoahongs!=null){
                         
                        $trahoahong->setXUAT_ID($xuat->getXUAT_ID());
                        $trahoahong->setLHH_ID($loaihoahongs->getLHH_ID());
                        $SOLUONG_= explode(',', $SOLUONG);
                        if(($loaihoahongs->getLHH_PHAN_TRAM()==0) && ($loaihoahongs->getLHH_TIEN_MAT()!=NULL)){
                            $SOTIEN=($loaihoahongs->getLHH_TIEN_MAT())*$SOLUONG_[0];
                        }else{
                            $SOTIEN=(($loaihoahongs->getLHH_PHAN_TRAM())/100)*$XUAT_TONG_TIEN_PHAI_THU;
                        }
                        $trahoahong->setSO_TIEN($SOTIEN);
                        $trahoahong->insert($db);
//                        echo $SOTIEN;
//                        exit();
                    }
                    
                    $chi=new Chi();
                    $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
                    $chi->setCHI_SO_TIEN($SOTIEN);
                    $chi->setCHI_NOI_DUNG("Chi loại hoa hồng: ".$loaihoahongs->getLHH_TEN_LOAI());
                    $chi->setCHI_PHAN_LOAI("hoahong");
                    $chi->setIS_ACTIVE(1);
                    $chi->insert($db);
                    
                    
                    $chihoahong=new Chi_hoa_hong();
                    $chihoahong->setCHI_ID($chi->getCHI_ID());
                    $chihoahong->setTHH_ID($trahoahong->getTHH_ID());
                    $chihoahong->setSO_TIEN($SOTIEN);
                    $chihoahong->insert($db);
//                    echo 'a';
//                    exit();
                }
                }
                //Kết thúc chi hoa hồng
                
                //Chi thuế
                $truong=new Truong();
                $truong->setTRUONG_ID($TRUONG_ID);
                $truong->getByID($db);
                if($truong!=null){
                   $bep=new Bep();
                   $bep->setBEP_ID($truong->getBEP_ID());
                   $bep->getByID($db);
                   if($bep !=null){
                       
                       if($bep->getBEP_IS_THUE()==1){
                            $SOTIEN_THUE=0;
                            $trathue=new Tra_thue();
                            $trathue->setXUAT_ID($xuat->getXUAT_ID());
                            $SOTIEN_THUE=$XUAT_TONG_TIEN_PHAI_THU*0.052;
                            $trathue->setSO_TIEN($SOTIEN_THUE);
                            $trathue->insert($db);
                            
                            
                            $chi=new Chi();
                            $chi->setCHI_NGAY_CHI($date->format("Y-m-d"));
                            $chi->setCHI_SO_TIEN($SOTIEN_THUE);
                            $chi->setCHI_NOI_DUNG("Chi cho thuế");
                            $chi->setCHI_PHAN_LOAI("thue");
                            $chi->setIS_ACTIVE(1);
                            $chi->insert($db);
                            
                            
                            $chithue=new Chi_thue();
                            $chithue->setCHI_ID($chi->getCHI_ID());
                            $chithue->setTT_ID($trathue->getTT_ID());
                            $chithue->setSO_TIEN($SOTIEN_THUE);
                            $chithue->insert($db);
                       }
                   }
                }
                
               
                
                //Kết thúc chi thuế
                
                
                
                
                
                $thu=new Thu();
                $thu->setNGAY_THU($date->format("Y-m-d"));
                $thu->setSO_TIEN($XUAT_TONG_TIEN_PHAI_THU);
                $thu->setIS_ACTIVE(1);
                $thu->insert($db);
                
                $thubancom=new Thu_ban_com();
                $thubancom->setTHU_ID($thu->getTHU_ID());
                $thubancom->setXUAT_ID($xuat->getXUAT_ID());
                $thubancom->setSO_TIEN($XUAT_TONG_TIEN_PHAI_THU);
                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietxuat=new Chi_tiet_xuat();
                $chitietxuat->setXUAT_ID($xuat->getXUAT_ID());
                $chitietxuat->setHH_ID($ID_HH_[$i]);
                $chitietxuat->setDON_GIA($DON_GIA_[$i]);
                $chitietxuat->setSO_LUONG($SOLUONG_[$i]);
                $chitietxuat->setTHANH_TIEN($THANHTIEN_[$i]);
                $chitietxuat->setIS_ACTIVE(1);
                $chitietxuat->insert($db);
            }
                
                
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
    private function getAllTruong() {
        $db = new Database();
        try {
            $truong = new Truong();

            $result = $truong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();
            $hanghoa->setMA_HH('com');
            $result = $hanghoa->getByMA_HH($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    private function deleteThue_HoaHong($values) {
        $db = new Database();
        try {            
           
                $trathue=new Tra_thue();
                $trathue->setXUAT_ID($values);
                $trathues=$trathue->getByID_Xuat($db);
                
                $chithue=new Chi_thue();
                $chithue->setTT_ID($trathues->getTT_ID());
                $chithues=$chithue->getByID($db);
                
                $chi=new Chi();
                $chi->setCHI_ID($chithues->getCHI_ID());
                
                
               
                
                //Delete chi thue
                $chithue_d=new Chi_thue();
                $chithue_d->setTT_ID($chithue->getTT_ID());
                $chithue_d->setCHI_ID($chithue->getCHI_ID());
                $chithue_d->delete($db);
                
                 //Delete chi
                $chi->delete($db);
                
                //Delete trả thuế
                $trathue_d=new Tra_thue();
                $trathue_d->setTT_ID($trathues->getTT_ID());
                $trathue_d->delete($db);
                
                
                //Delete Hoa hồng
                
                $trahoahong=new Tra_hoa_hong();
                $trahoahong->setXUAT_ID($values);
                $trahoahongs=$trahoahong->getByID_Xuat($db);
                 if ($trahoahongs != null) {
                 
                 foreach ($trahoahongs as $trahoahong_) {
                     $chihoahong=new Chi_hoa_hong();
                     $chihoahong->setTHH_ID($trahoahong_->getTHH_ID());
                     $chihoahongs=$chihoahong->getByID($db);
                     
                     
                     $chi=new Chi();
                     $chi->setCHI_ID($chihoahongs->getCHI_ID());
                     
                     
                     //Delele hoa hồng
                      $chihoahong_d=new Chi_hoa_hong();
                     $chihoahong_d->setTHH_ID($chihoahongs->getTHH_ID());
                     $chihoahong_d->setCHI_ID($chihoahongs->getCHI_ID());
                     $chihoahong_d->delete($db);
                     
                     $chi->delete($db);
                     
                     $trahoahong_d=new Tra_hoa_hong();
                     $trahoahong_d->setTHH_ID($trahoahong_->getTHH_ID());
                     $trahoahong_d->delete($db);
                     
                    }
                 
                 }
                
                
                
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
        private function deleteThu($values) {
        $db = new Database();
        try {            
           
                
            
                $trathue=new Tra_thue();
                $trathue->setXUAT_ID($values);
                $trathues=$trathue->getByID_Xuat($db);
                
                $chithue=new Chi_thue();
                $chithue->setTT_ID($trathues->getTT_ID());
                $chithues=$chithue->getByID($db);
                
                $chi=new Chi();
                $chi->setCHI_ID($chithues->getCHI_ID());
                
                
                
                
                //Delete Thuế
                $chithue_d=new Chi_thue();
                $chithue_d->setTT_ID($chithue->getTT_ID());
                $chithue_d->setCHI_ID($chithue->getCHI_ID());
                $chithue_d->delete($db);
                
                $chi->delete($db);
                
                $trathue_d=new Tra_thue();
                $trathue_d->setTT_ID($trathues->getTT_ID());
                $trathue_d->delete($db);
                
                //Delete Hoa hồng
                
                $trahoahong=new Tra_hoa_hong();
                $trahoahong->setXUAT_ID($values);
                $trahoahongs=$trahoahong->getByID_Xuat($db);
                 if ($trahoahongs != null) {
                 
                 foreach ($trahoahongs as $trahoahong_) {
                     $chihoahong=new Chi_hoa_hong();
                     $chihoahong->setTHH_ID($trahoahong_->getTHH_ID());
                     $chihoahongs=$chihoahong->getByID($db);
                     
                     
                     $chi=new Chi();
                     $chi->setCHI_ID($chihoahongs->getCHI_ID());
                     
                     
                     //Delele hoa hồng
                      $chihoahong_d=new Chi_hoa_hong();
                     $chihoahong_d->setTHH_ID($chihoahongs->getTHH_ID());
                     $chihoahong_d->setCHI_ID($chihoahongs->getCHI_ID());
                     $chihoahong_d->delete($db);
                     
                     $chi->delete($db);
                     
                     $trahoahong_d=new Tra_hoa_hong();
                     $trahoahong_d->setTHH_ID($trahoahong_->getTHH_ID());
                     $trahoahong_d->delete($db);
                     
                    }
                 
                 }
                //Delete thu
                $thu = new Xuat();
                $thu->setXUAT_ID($values);
                $thu->delete($db);
                
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    
}
